package com.tavio.tbs.unit;

import org.andengine.entity.sprite.Sprite;

import com.tavio.tbs.structure.Position;
import com.tavio.tbs.terrain.TerrainType;
import com.tavio.tbs.world.WorldMap;

public class Soldier extends Unit{
	public Soldier(Position position, int stepsAvailable, Sprite sprite) {
		this.position = position;
		this.stepsAvailable = stepsAvailable;
		this.sprite = sprite;
	}

	@Override
	protected boolean isPositionValid(Position position) {
		if(position.getX() > WorldMap.getInstance().getNumColumns() -1 || position.getY() > WorldMap.getInstance().getNumRows() -1 || position.getX() < 0 || position.getY() < 0)
			return false;
		return true;	
	}

	@Override
	protected int calculateCostForTerrain(int stepsRemaining, Position terrainPosition,TerrainType terrain) {
		
		/* If the unit is right next to the terrain it wants to move to and the terrain is valid for this unit, then
		 * the unit should be able to move to it, even if the terrain cost is higher than the steps available for this unit.
		 * For instance, an infantry unit with only 2 steps available should be able to climb to a mountain that costs
		 * 3 movement units if it's standing right next to the mountain. 
		 */
		if(stepsRemaining == this.stepsAvailable && 
				stepsRemaining < terrain.getMovementCost() &&
				(this.position.getX() == terrainPosition.getX() && this.position.getY() == terrainPosition.getY() - 1 ||
				this.position.getX() == terrainPosition.getX() && this.position.getY() == terrainPosition.getY() + 1 ||
				this.position.getX() == terrainPosition.getX() - 1 && this.position.getY() == terrainPosition.getY() ||
				this.position.getX() == terrainPosition.getX() + 1 && this.position.getY() == terrainPosition.getY())) {
			return stepsAvailable;
		}
		
		return terrain.getMovementCost();
	}
}
